import { ListResult } from '@/data/result';
import { list } from '@/services/data';
import { convertListToValueEnum } from '@/utils';
import useSWR from 'swr';

const fetcher = async () => {
  const data: ListResult = await list('/system/index/tTenant');
  const allTenantValueEnum = convertListToValueEnum(data?.list, 'id', 'name');

  return { allTenantValueEnum };
};

export const useTenants = () => {
  const { data, isLoading, mutate } = useSWR('/system/index/tTenant', fetcher, {
    revalidateOnFocus: false, // 默认不重新验证
    revalidateOnMount: true, // 组件挂载时验证
    revalidateIfStale: true, // 数据过期时重新验证
    dedupingInterval: 30000, // 30秒内重复请求返回缓存数据
    refreshInterval: 0, // 不自动刷新
  });
  const { allTenantValueEnum = {} }: any = data || {};

  return {
    allTenantValueEnum,
    isLoading,
    mutate,
  };
};
